package com.kla.power.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kla.power.entity.MerchantEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商户/部门表 Mapper 接口
 * </p>
 * @author WuZH
 * @since 2023-06-19
 */
@Mapper
public interface MerchantMapper extends BaseMapper<MerchantEntity> {

    @Select ( value = "SELECT DISTINCT c1.level - 1 level, c2.id, c2.parentId, c2.merchantName, c2.merchantDesc " +
                        "FROM (" +
                        "         SELECT @ids as _ids, " +
                        "                (SELECT @ids := GROUP_CONCAT(id) " +
                        "                 FROM merchant_info " +
                        "                 WHERE FIND_IN_SET(parentId, @ids) " +
                        "                )            as cids, " +
                        "                @l := @l + 1 as level " +
                        "         FROM merchant_info, " +
                        "              (SELECT @ids := #{merchantEntity.id}, @l := 0) b " +
                        "     ) c1, " +
                        "     merchant_info c2 " +
                        "WHERE FIND_IN_SET(c2.id, c1._ids) AND c2.isUsed=1 " +
                        "ORDER BY level, id")
    List<MerchantEntity> queryList (@Param ("merchantEntity") MerchantEntity merchantEntity);

}
